/**
 * Menu
 *
 * A window with a list of action
 */
class Menu extends Window
{
  // Event fired when the user select some item from
  // the list, the data is the item selected
  public final static int EVENT_ITEM_SELECTED = 1;

  // The midlet owner of the window
  PMIDlet pMIDlet;

  // The items font
  PFont font = loadFont();

  // Name of the menu
  String name;  
  
  // Items a index of the selection
  int numItems;
  String[] items;
  int selectedItem;

  /**
   * Create a Menu with the parent and name specified
   *
   * @param pMIDlet parent of the window
   * @param name Menu name
   */
  Menu(PMIDlet pMIDlet, String name)
  {
    this.pMIDlet = pMIDlet;
    this.name = name;
    
    // Create a item list of two to 
    // allow expand the array
    items = new String[2];
    
    // Calculate initial size
    textFont(font);
    width = calculateWidth(name);
    height = 2*font.height;
  }

  /**
   * Draw the menu
   */
  void draw()
  { 
    // Save transformation
    pushMatrix();   
    
    // Set text attributes
    textFont(font);
    textAlign(CENTER);

    // Move to the corner
    translate(x,y);

    // Draw and rect with stroke
    fill(0x000000);
    rect(0,0,width,height);

    // Draw the menu name
    int tmpY = font.height;
    fill(0xFFFF00);    
    text(name,width/2,tmpY);

    // Draw each item 
    for(int i=0; i<numItems; i++)
    {
      tmpY += font.height;

      // Draw the selected item in white
      if(i == selectedItem)
        fill(0xFFFFFF);
      // Other items in green
      else
        fill(0x00FF00);

      text(items[i],width/2,tmpY);
    }

    // Restore transformation
    popMatrix();
  }

  /**
   * Update user selection
   */
  void keyPressed()
  {
    switch(keyCode)
    {
    // User moves to item above, if first one do nothing
    case UP :
      if(selectedItem > 0)
        selectedItem--;
      break;
    // User moves to item below, if last one do nothing
    case DOWN :
      if(selectedItem + 1 < numItems)
        selectedItem++;
      break;
    // User select an item, fire the event
    case FIRE :
      pMIDlet.enqueueLibraryEvent(this,
        EVENT_ITEM_SELECTED,items[selectedItem]);
    }
  }

  /**
   * Calculate the new width of the window according the 
   * text to show.
   *
   * If the text is greater than the current window width return 
   * the new width else return the window width
   *
   * @param text Item to add
   * @return the new width of the window 
   */
  int calculateWidth(String text)
  {
    // Calculate item width, plus some space
    int tmpWidth = textWidth(text) + 10;

    // If the width is greater than current width
    // return the item width
    if(tmpWidth > width)
      return tmpWidth;

    // Mantain the current width
    return width;
  }  

  /**
   * Add a new item to the menu
   *
   * @param item Item to add
   */
  void add(String item)
  {
    // If the array is full, expand it
    if(numItems == length(items))
      items = expand(items);

    // Add to the array and update counter
    items[numItems++] = item;    

    // Update the window size
    width = calculateWidth(item);    
    height += font.height;
  }
  
  /**
   * Delete the item in the specified index
   *
   * @param index Item index to delete
   */
  void deleteItem(int index)
  {
    // Move the items over the removed
    for(int i=index; i<numItems-1; i++)
      items[i] = items[i+1];
      
    // Update counter and update height 
    numItems--;
    height -= font.height;
  }
}
